package com.fu.table03;

import com.fu.bean.Classes;
import com.fu.bean.Course;
import com.fu.bean.Student;
import com.fu.table02.OneToManyMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 付大大
 * @description
 * @create 2021-02-01 17:27
 */
public class Test01 {

    @Test
    public void selectAll() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        ManyToManyMapper mapper = sqlSession.getMapper(ManyToManyMapper.class);
        List<Student> students = mapper.selectAll();

        for (Student student : students) {
            System.out.println(student.getId() + "," + student.getName() + "," + student.getAge());
            List<Course> courses = student.getCourses();
            for (Course cours : courses) {
                System.out.println("\t"+cours);
            }
        }


        sqlSession.close();
        resourceAsStream.close();
    }
}
